# -*- coding: utf-8 -*-

# Define here the models for your spider middleware
#
# See documentation in:
# https://doc.scrapy.org/en/latest/topics/spider-middleware.html

from scrapy import signals
import random
from scrapy.http import HtmlResponse

class SlavegoodreadSpiderMiddleware(object):
    # Not all methods need to be defined. If a method is not defined,
    # scrapy acts as if the spider middleware does not modify the
    # passed objects.

    @classmethod
    def from_crawler(cls, crawler):
        # This method is used by Scrapy to create your spiders.
        s = cls()
        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
        return s

    def process_spider_input(self, response, spider):
        # Called for each response that goes through the spider
        # middleware and into the spider.

        # Should return None or raise an exception.
        return None

    def process_spider_output(self, response, result, spider):
        # Called with the results returned from the Spider, after
        # it has processed the response.

        # Must return an iterable of Request, dict or Item objects.
        for i in result:
            yield i

    def process_spider_exception(self, response, exception, spider):
        # Called when a spider or process_spider_input() method
        # (from other spider middleware) raises an exception.

        # Should return either None or an iterable of Response, dict
        # or Item objects.
        pass

    def process_start_requests(self, start_requests, spider):
        # Called with the start requests of the spider, and works
        # similarly to the process_spider_output() method, except
        # that it doesn’t have a response associated.

        # Must return only requests (not items).
        for r in start_requests:
            yield r

    def spider_opened(self, spider):
        spider.logger.info('Spider opened: %s' % spider.name)


class SlavegoodreadDownloaderMiddleware(object):
    # Not all methods need to be defined. If a method is not defined,
    # scrapy acts as if the downloader middleware does not modify the
    # passed objects.

    @classmethod
    def from_crawler(cls, crawler):
        # This method is used by Scrapy to create your spiders.
        s = cls()
        crawler.signals.connect(s.spider_opened, signal=signals.spider_opened)
        return s

    def process_request(self, request, spider):
        # Called for each request that goes through the downloader
        # middleware.

        # Must either:
        # - return None: continue processing this request
        # - or return a Response object
        # - or return a Request object
        # - or raise IgnoreRequest: process_exception() methods of
        #   installed downloader middleware will be called
        return None
        '''
        proxy_list = [
        '37.32.127.183:52519',
        '46.209.30.157:8080',
        '91.98.101.44:59052',
        '217.146.209.25:8080',
        '178.239.147.131:56133',
        '5.160.14.130:54039',
        '82.99.221.18:53281',
        '185.112.36.215:37524',
        '37.32.127.161:39934',
        '93.126.28.112:8080',
        ]
        request.meta['proxy'] = 'http://' + random.choice(proxy_list)
        return HtmlResponse(url=request.url, request=request, encoding='utf-8')
        '''
        
    def process_response(self, request, response, spider):
        # Called with the response returned from the downloader.

        # Must either;
        # - return a Response object
        # - return a Request object
        # - or raise IgnoreRequest
        return response

    def process_exception(self, request, exception, spider):
        # Called when a download handler or a process_request()
        # (from other downloader middleware) raises an exception.

        # Must either:
        # - return None: continue processing this exception
        # - return a Response object: stops process_exception() chain
        # - return a Request object: stops process_exception() chain
        pass

    def spider_opened(self, spider):
        spider.logger.info('Spider opened: %s' % spider.name)

class HttpbinProxyMiddleware(object):
    def process_request(self, request, spider):
        proxy_list = [
        '185.42.225.162:54902',
        '5.202.253.2:8080',
        '5.202.253.4:8080',
        '37.235.28.95:40492',
        '185.129.212.25:50505',
        '46.225.248.10:8080',
        '80.253.152.210:8080',
        '46.209.30.155:8080',
        '217.146.209.11:8080',
        '79.127.104.185:8080',
        '46.209.58.186:32231',
        '185.101.228.219:49685',
        '46.209.150.82:8080',
        '37.255.211.111:48841',
        '79.174.166.44:50899',
        '5.202.191.242:8080',
        '84.241.15.23:23500',
        '92.50.31.125:30455',
        '89.43.6.114:8080',
        '185.142.94.195:41258',
        '2.188.25.133:8080',
        '94.101.182.156:8080',
        '46.209.150.88:8080',
        '91.98.101.44:59052',
        '37.235.28.42:32935',
        '217.146.209.37:8080',
        '185.83.182.238:36257',
        '81.16.112.130:8080',
        '188.136.143.82:37819',
        '217.146.209.20:8080',
        '46.209.58.188:32231',
        '185.112.36.215:37524',
        '85.133.207.14:56728',
        '217.146.209.44:8080',
        '185.66.224.83:80',
        '178.252.166.122:32769',
        '185.161.38.254:38597',
        '78.38.180.21:8080',
        '217.146.209.18:8080',
        '185.129.212.75:8080',
        '82.99.228.157:8080',
        '109.95.64.32:32231',
        '85.133.130.75:57829',
        '46.209.30.158:8080',
        '185.236.36.132:57499',
        '185.237.87.218:33480',
        '80.75.14.218:8080',
        '83.147.251.34:32231',
        '178.239.147.131:56133',
        '5.202.176.18:30641',
        '185.66.224.83:80',
        '5.160.14.130:54039',
        '188.75.82.101:40387',
        '46.100.58.6:8080',
        '92.50.31.125:30455',
        '89.221.85.98:8888',
        '5.202.176.18:30641',
        '2.188.171.80:8888',
        '82.99.228.153:8080',
        '78.38.180.21:8080',
        '5.202.114.4:39597',
        '84.241.15.23:23500',
        '185.112.35.28:52478',
        '93.118.102.82:8080',
        '84.241.24.67:23500',
        '93.126.28.112:8080',
        '46.245.38.239:58610',
        '2.188.25.133:8080',
        '94.74.134.186:80',
        '185.129.212.25:50505',
        '79.127.104.185:8080',
        '217.146.209.20:8080',
        '2.185.214.182:8888',
        '82.99.228.149:8080',
        '217.146.209.33:8080',
        '46.100.58.6:8080',
        '217.146.208.181:8080',
        '5.202.191.245:8080',
        '217.146.209.10:8080',
        '80.75.14.218:8080',
        '5.202.32.11:45966',
        '46.209.150.83:8080',
        '217.146.209.15:8080',
        '185.137.60.18:40172',
        '79.127.104.186:8080',
        '46.209.30.157:8080',
        '85.133.207.14:56728',
        '5.78.164.195:8080',
        '185.213.171.239:23500',
        '185.42.25.62:58125',
        '37.32.127.183:52519',
        '46.225.248.11:8080',
        '85.9.75.63:35279',
        '2.188.25.133:8080',
        '185.129.212.25:50505',
        '185.83.182.238:36257',
        '185.42.225.162:54902',
        '178.252.166.122:32769',
        '46.225.234.117:23500',
        '185.176.56.37:38748',
        '217.146.209.34:8080',
        '89.221.85.98:59014',
        '185.176.56.80:61677',
        '185.112.36.215:37524',
        '46.245.38.239:58610',
        '217.146.209.11:8080',
        '37.235.28.42:32935',
        '84.241.24.67:23500',
        '5.160.14.130:54039',
        '84.241.15.23:23500',
        '217.146.209.45:8080',
        '188.75.82.101:40387',
        '217.146.209.25:8080',
        '185.186.242.192:58352',
        '46.209.150.87:8080',
        '79.174.166.135:58574',
        '188.136.143.82:37819',
        '188.136.255.161:46201',
        '185.112.37.33:53281',
        '84.241.22.20:23500',
        '5.201.134.229:8080',
        '5.202.191.245:8080',
        '94.74.181.112:80',
        '217.146.209.34:8080',
        '82.99.228.150:8080',
        '92.50.28.26:8080',
        '85.9.75.63:35279',
        '37.235.16.186:41258',
        '188.214.232.2:8080',
        '185.236.36.132:57499',
        '37.152.163.49:53312',
        '37.152.184.133:8080',
        '46.100.58.189:49861',
        '185.237.87.218:33480',
        '217.146.209.20:8080',
        '185.129.212.8:8080',
        '82.99.228.147:8080',
        '185.161.36.226:8080',
        '46.209.150.88:8080',
        '217.146.209.25:8080',
        '94.74.134.186:80',
        '84.241.22.52:23500',
        '83.147.220.217:53281',
        '185.161.38.254:38597',
        '185.129.212.25:50505',
        '185.153.209.161:42419',
        '185.82.67.134:52902',
        '46.100.58.6:8080',
        '37.235.16.157:41258',
        '78.111.15.121:51286',
        '5.202.176.18:30641',
        '5.202.253.2:8080',
        '178.173.165.84:53281',
        '5.160.14.130:54039',
        '31.7.142.180:39168',
        '2.180.36.99:32996',
        '185.83.182.238:36257',
        '185.112.36.112:38846',
        '2.188.171.80:8888',
        '185.137.60.18:40172',
        '46.225.234.126:23500',
        '80.253.152.210:8080',
        '46.225.248.10:8080',
        '31.29.62.115:36526',
        '81.16.112.130:8080',
        '85.133.130.75:57829',
        '217.146.209.11:8080',
        '217.146.209.13:8080',
        '91.186.209.82:40284',
        '5.202.191.243:8080',
        '46.245.38.239:58610',
        '185.112.37.33:53281',
        '185.180.130.43:8080',
        '46.225.248.11:8080',
        '77.237.90.251:8080',
        '46.209.150.82:8080',
        '178.252.166.122:32769',
        '82.99.228.156:8080',
        '5.202.52.82:47030',
        '92.50.48.194:8080',
        '46.209.30.155:8080',
        '217.146.209.37:8080',
        '82.99.228.157:8080',
        '185.49.97.41:44151',
        '217.146.209.17:8080',
        '82.99.228.153:8080',
        '2.187.39.181:42644',
        '83.147.220.5:53281',
        '185.186.242.192:58352',
        '84.241.42.39:23500',
        '109.95.71.254:32231',
        '185.112.36.147:34641',
        '185.3.213.61:53281',
        '89.43.6.114:8080',
        '46.209.150.81:8080',
        '5.200.93.5:57439',
        '79.127.104.185:8080',
        '93.110.122.53:53281',
        '46.209.30.156:8080',
        '217.146.209.23:8080',
        '185.162.218.18:39945',
        '5.160.167.234:38313',
        '77.104.75.202:39761',
        '46.209.58.186:32231',
        '80.75.14.218:8080',
        '5.202.192.147:8080',
        '94.101.182.156:8080',
        '217.146.209.14:8080',
        '91.98.101.44:59052',
        '46.225.234.115:23500',
        '217.146.209.45:8080',
        '217.146.209.33:8080',
        '37.235.28.95:40492',
        '31.29.61.37:40827',
        '46.225.234.125:23500',
        '79.174.166.44:50899',
        '5.160.229.110:56398',
        '46.209.112.188:31392',
        '178.22.124.206:43502',
        '77.237.87.77:45301',
        ]
        request.meta['proxy'] = 'http://' + random.choice(proxy_list)
